﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.DTOs.BatchManagementDTOs;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;


namespace U.Enterprise.Api.Applications.BatchManagements
{
    /// <summary>
    /// 批宰选择
    /// </summary>
    public class SlaughterSelectionHander : IRequestHandler<SlaughterSelectionCommand, Resqults<SlaugSelectDTO>>
    {
        private readonly IBateRepository<Approach> approch;
        private readonly IBateRepository<LiveAnimalweighing> live;
        private readonly IBateRepository<SlaughterCircle> slaug;
        private readonly IBateRepository<BulkSlaughter> bulk;

        public SlaughterSelectionHander(IBateRepository<Approach> approch, IBateRepository<LiveAnimalweighing> live, IBateRepository<SlaughterCircle> slaug, IBateRepository<BulkSlaughter> bulk)
        {
            this.approch = approch;
            this.live = live;
            this.slaug = slaug;
            this.bulk = bulk;
        }

        public async Task<Resqults<SlaugSelectDTO>> Handle(SlaughterSelectionCommand request, CancellationToken cancellationToken)
        {
            try
            {

                var gh = approch.GetAll();

                var vh= live.GetAll();


                Resqults<SlaugSelectDTO> res = new Resqults<SlaugSelectDTO>();
                SlaugSelectDTO slaugSelectDTO = new SlaugSelectDTO();
                var lsit =  from a in approch.GetAll()
                            join b in live.GetAll() on a.ApproachNo equals b.ApproachNo
                            join c in slaug.GetAll() on b.SlaughterCircleId equals c.SlaughterCircledId
                            select new SlaugSelectDTO()
                            {
                                LiveAnimalweighingId = b.LiveAnimalweighingId,
                                Suppliers = a.Suppliers,
                                ApproachTime = a.ApproachTime,
                                ApproachNo = a.ApproachNo,
                                ApproachBatch = a.ApproachBatch,
                                //SlaughterCircleName = c.SlaughterCircleName,
                                Num = b.SlaughterCircleNum,
                                AverageWeight = b.AverageWeight,
                                ShoppingDesignationid = a.ShoppingDesignationid,
                                WeighTime=b.WeighTime,
                                Time = 0,
                                Raemk = b.Remark
                            };

                if (!string.IsNullOrEmpty(request.Suppliers))
                {
                    lsit = lsit.Where(x => x.Suppliers == request.Suppliers);
                }
                if (request.ApproachTime != null)
                {
                    lsit = lsit.Where(x => x.ApproachTime == Convert.ToDateTime(request.ApproachTime));
                }
                if (!string.IsNullOrEmpty(request.ApproachBatch))
                {
                    lsit = lsit.Where(x => x.ApproachBatch.Contains(request.ApproachBatch));
                }
                var list= lsit.ToList();
                foreach (var item in list)
                {
                    item.Time = (DateTime.Now - item.WeighTime).Hours;
                    var bulkList = bulk.GetAll().Where(x => x.LiveAnimalweighingId == item.LiveAnimalweighingId).FirstOrDefault();
                    if (bulkList != null)
                    {
                        item.Num -= bulkList.BulkSlaughterNum;
                    }

                }
                res.Date = lsit.ToList();
                return res;
            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }
}
